<?php
namespace app\controller\manager;

use app\BaseController;
use app\model\Student;
use app\model\VStudentInfo;
use app\utils\Excel;
use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Size;
use \think\facade\Db;
use think\Request;

/**
 * 学生管理控制器
 * @todo 完善增删改查
 * @todo 完善excel导入
 */
class StudentManage extends BaseController
{
    
    public function list(){
        $pageSize = $this->request->param('pageSize',10);
        $academyID = $this->request->param('academyID','not null');
        $majorID = $this->request->param('majorID','not null');
        $claseID = $this->request->param('claseID','not null');
        $SID = $this->request->param('SID','not null');
        $student = VStudentInfo::where('SID',$SID);
        if($academyID != 'not null'){
            $student->where('academyID',$academyID);
        }
        if($majorID != 'not null'){
            $student->where('majorID',$majorID);
        }
        if($claseID != 'not null'){
            $student->where('claseID',$claseID);
        }
        $student=$student->paginate($pageSize);                                       
        return resultSuccess($student);
    }

    /**
     * 获取学生名字
     */
    public function getStudentName(){
        $SID = $this->request->param('SID');
        if($SID==null) return resultError('请输入学号');
        $student = VStudentInfo::where('SID',$SID)->find();
        if($student == null){
            return resultError('无此学生');
        }
        return resultSuccess($student);
    }

     /**
     * 增加学生
     */
    public function addStudent(Request $request)
    {
        if ($request->isPost()) {
            $student = new Student;
            if ($student->save(
                [
                    'SID' => $request->param('SID'),
                    'claseID' => $request->param('claseID'),
                    'name' => $request->param('name'),
                    'sex' => $request->param('sex'),
                    'password' => \password_hash($request->param('SID'),PASSWORD_DEFAULT)
                ]
            )) {
                return resultSuccess(null,"成功");
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 删除学生
     */
    public function deleteStudent(Request $request)
    {
        if ($request->isGet()) {
            $student = Student::find($request->param('SID'));
            if ($student->delete()) {
                return resultSuccess(null,"成功");
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 修改学生信息
     */
    public function editStudent(Request $request)
    {
        if ($request->isPost()) {
            $student = Student::find($request->param('SID'));
            if ($student->save(
                [
                    'claseID' => $request->param('claseID'),
                    'name' => $request->param('name'),
                    'sex' => $request->param('sex'),
                ]
            )) {
                return resultSuccess(null,"成功");
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * Excel插入学生(覆盖插入)
     */
    public function excelInsertStudent(Request $request){
        $claseID = $request->param('claseID');
        if(!$claseID) return resultError('需要班级ID');
        $files = $request->file("file");
        $data = Excel::importExcel($files);
        $insertList = [];
        foreach($data as $dt){
            $password = password_hash($dt[0],PASSWORD_DEFAULT);
            $insertList[]=[
                'SID' => $dt[0],
                'name' => $dt[1],
                'sex' => $dt[2]=='男'?0:1,
                'claseID' => $claseID,
                'password' => $password
            ];
        }
        try{
        if(Db::name('student')->replace()->insertAll($insertList) != sizeof($insertList)){
            return resultError('未能全部插入');
        }
        }
        catch( Exception $e){
            return resultError('格式错误');
        }
        return resultSuccess();
    }


    
}
